﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace PimImporter
{
    /// <summary>
    /// This form manage mapping between phone fields and the others in csv file
    /// </summary>
    internal partial class FrmMapping : Form
    {
        private List<string> headers = null;
        private int numCols = -1;
        private static bool init;
        private CsvReader reader;
        private List<string> listFields;
        private List<string> listFieldsOrdered;
        private Dictionary<string, int> mappingList;

        public FrmMapping(List<string> headers, int numCols, CsvReader reader, ref Dictionary<string, int> mappingList)
        {
            InitializeComponent();
            this.headers = headers;
            this.numCols = numCols;
            this.reader = reader;
            this.mappingList = mappingList;
            Init();
        }

        private void Init()
        {
            init = true;
            int n;
            listFields = new List<string>();
            if (headers == null)
            {
                for (int i = 0; i < numCols; i++)
                {
                    listFields.Add("Column" + i);
                }
                n = numCols;
            }
            else
            {
                for (int i = 0; i < headers.Count; i++)
                {
                    if (headers[i] != "")
                    {
                        listFields.Add(headers[i]);
                    }
                }
                n = headers.Count;
            }
            listFieldsOrdered = new List<string>(listFields);
            listFieldsOrdered.Sort();
            listFieldsOrdered.Insert(0, "-");
            int resolution = Convert.ToInt32(this.AutoScaleDimensions.Height);
            int y = Def.ResizeResolution(23, resolution);
            for (int i = 0; i < n; i++)
            {
                XElement elem = FrmMain.mapper.lista[i];
                ComboBox comboP = new ComboBox();
                comboP.Name = "comboP" + i;
                comboP.Left = 2;
                comboP.Top = y;
                comboP.Width = Def.ResizeResolution(155, resolution);
                List<string> listaNew = new List<string>();
                listaNew.Add(elem.Name.LocalName);
                comboP.DataSource = listaNew;
                this.Controls.Add(comboP);
                ComboBox comboE = new ComboBox();
                comboE.Name = "comboE" + i;
                comboE.Left = LblExternal.Left;
                comboE.Top = y;
                comboE.Width = Def.ResizeResolution(146, resolution);
                comboE.DataSource = new List<string>(listFieldsOrdered);
                comboE.SelectedIndexChanged += new EventHandler(comboF_SelectedIndexChanged);
                this.Controls.Add(comboE);
                if (elem.Value != "" && listFieldsOrdered.Contains(elem.Value))
                {
                    comboE.SelectedIndex = listFieldsOrdered.IndexOf(elem.Value);
                }
                else
                {
                    comboE.SelectedIndex = 0;
                }
                y += Def.ResizeResolution(28, resolution);
            }
            init = false;
        }

        private void comboF_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!init)
            {
                foreach (var c in this.Controls)
                {
                    if (c.GetType() == typeof(ComboBox) && (c as ComboBox).Name != (sender as ComboBox).Name && (c as ComboBox).Name.StartsWith("comboE") && (c as ComboBox).Text == (sender as ComboBox).Text)
                    {
                        (c as ComboBox).SelectedIndex = 0;
                        return;
                    }
                }
            }
        }

        private void MniCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void MniOk_Click(object sender, EventArgs e)
        {
            List<string> listTmp = new List<string>();
            foreach (var c in this.Controls)
            {
                if (c.GetType() == typeof(ComboBox) && (c as ComboBox).Name.StartsWith("comboE") && (c as ComboBox).Text != "-")
                {
                    listTmp.Add((c as ComboBox).Text);
                }
            }
            List<int> listNonMapped = (from c in listFields
                                       where !listTmp.Contains(c)
                                       select listFields.IndexOf(c)).ToList();
            List<int> listResult = reader.TestColumns(listNonMapped);
            if (listResult.Count != 0)
            {
                List<string> listResName = (from c in listResult
                                            select listFields[c]).ToList();
                string message = "These columns contains data and are not mapped. Do you want lose this data?\r\n";
                foreach (var c in listResName)
                {
                    message += c + "\r\n";
                }
                if (MessageBox.Show(message, "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                {
                    return;
                }
            }
            for (int i = 2; i < this.Controls.Count; i++)
            {
                Control c1 = this.Controls[i];
                Control c2 = this.Controls[i + 1];
                if ((c2 as ComboBox).Text != "-")
                {
                    mappingList.Add(c1.Text, listFields.IndexOf(c2.Text));
                }
                i++;
            }
            this.DialogResult = DialogResult.OK;
        }
    }
}